$(function () {
    //常用jQuery对象
    var studentOfficial_datagrid = $("#studentOfficial_list");
    var studentOfficial_dailog = $("#studentOfficial_dialog");
    var studentOfficial_form = $("#studentOfficial_form");
    var resetPassword_dialog = $("#resetPassword_dialog");
    var changeClass_dialog = $("#changeClass_dialog");
    var changeClass_form = $("#changeClass_form");
    //=============================
    var leaving_dialog = $("#leaving_dialog");
    var leaving_form = $("#leaving_form");
    var msgDialog = $("#msgDialog");
    var sendMsg_form = $("#sendMsg_form");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {

            studentOfficial_dailog.dialog("open");
            studentOfficial_dailog.dialog("setTitle", "新增");
            $("#password").show();
        },

        //编辑按钮方法
        edit: function () {
            var row = studentOfficial_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }

            studentOfficial_dailog.dialog("open");
            studentOfficial_dailog.dialog("setTitle", "编辑");

            $.get("/studentofficial/selectByPrimaryKey.do",{id:row.id},function (data) {
                row=data;
                if(row.classname){
                    row["classname.id"]=row.classname.id;
                }
                if(row.clientName){
                    row["clientName.id"]=row.clientName.id;
                    row["clientName.name"]=row.clientName.name;
                }
                if (row.payType) {
                    row["payType.id"] = row.payType.id;
                }
                if (row.source) {
                    row["source.id"] = row.source.id;
                }
                if (row.campus) {
                    row["campus.id"] = row.campus.id;
                }
                if(row.saleman){
                    row["saleman.id"]=row.saleman.id;;
                }
                if (row.edubackground) {
                    row["edubackground.id"] = row.edubackground.id;
                }
                if (row.foreignlevel) {
                    row["foreignlevel.id"] = row.foreignlevel.id;
                }
                if (row.clientState) {
                    row["clientState.id"] = row.clientState.id;
                }
                if(row.clientType){
                    row["clientType.id"]=row.clientType.id;
                }
                console.log(row);
                studentOfficial_dailog.form("load", row);
            }, "json");
            $("form input").prop("readonly", false);

        },
        //导出文件模板
        getImportXlsMode:function () {
            var keyword = $("#keyword").textbox("getValue");
            var classNamId = $("#classNamId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            var qos = "keyword="+keyword+"&classNamId="+classNamId+"&beginDate="+beginDate+"&endDate"+endDate;
            window.open("/studentOfficialloyee/getImportXlsMode.do?qo="+qos);
        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            studentOfficial_form.form("submit", {
                url: "/studentofficial/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            studentOfficial_dailog.dialog("close");
                            studentOfficial_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            studentOfficial_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        //设置学员转班按钮
        changeClass:function(){
            var row = studentOfficial_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            if(!row.clientName){
                $.messager.alert("温馨提示","数据异常,学员名称不能为空!!!塞利木鸡拜");
                return;
            }
            if((row.clientState.id != 42)){
                $.messager.alert("温馨提示","学员状态异常");
                return;
            }
            changeClass_dialog.dialog("open");
            row["classname.name"]=row.classname.name;
            changeClass_dialog.form("load",row);
            changeClass_dialog.dialog("setTitle", "转班");
        },

        changeClassOK:function () {
            changeClass_form.form("submit", {
                url: "/studentofficial/change2class.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            changeClass_dialog.dialog("close");
                            studentOfficial_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            changeClass_dialog.dialog("close");
                        });
                    }
                }
            })
        },


        see: function () {
            var row = studentOfficial_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            studentOfficial_dailog.dialog("open");
            studentOfficial_dailog.dialog("setTitle", "查看");
            studentOfficial_dailog.form("load", row);
            $.get("/studentofficial/selectByPrimaryKey.do",{id:row.id},function (data) {
                row=data;
                if(row.classname){
                    row["classname.id"]=row.classname.id
                }
                if(row.clientName){
                    row["clientName.name"]=row.clientName.name;
                }
                if(row.payType){
                    row["payType.id"]=row.payType.id;
                }
                if(row.source){
                    row["source.id"]=row.source.id;
                }
                if(row.campus){
                    row["campus.id"]=row.campus.id;
                }
                if(row.saleman){
                    row["saleman.id"]=row.saleman.id;;
                }
                if(row.edubackground){
                    row["edubackground.id"]=row.edubackground.id;
                }
                if(row.foreignlevel){
                    row["foreignlevel.id"]=row.foreignlevel.id;
                }
                if(row.clientState){
                    row["clientState.id"]=row.clientState.id;
                }
                if(row.clientType){
                    row["clientType.id"]=row.clientType.id;
                }
                studentOfficial_dailog.form("load",row);
            },"json");
                $("form input").prop("readonly",true);


        },

        //刷新页面
        reloadList: function () {
            $("#keyword").textbox("setValue", "");
            $("#classNamId").combobox("setValue", "");
            var beginDate = $("#beginDate").datebox("setValue", "");
            var endDate = $("#endDate").datebox("setValue", "");
            studentOfficial_datagrid.datagrid("reload", {classNamId: "-1"});
        },

        //学员流失
        lost: function () { // 流失操作
            var formalStudents = studentOfficial_datagrid.datagrid("getSelected");
            console.log(formalStudents);
            if (!formalStudents) {
                $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                return;
            }
            if(!formalStudents.clientName){
                $.messager.alert("温馨提示","数据异常,学员名称不能为空!!!塞利木鸡拜");
                return;
            }
            if((formalStudents.clientState.id != 42)){
                $.messager.alert("温馨提示","学员状态异常");
                return;
            }
            if((formalStudents.clientState.id == 43 || formalStudents.clientState.id == 44)){
                $.messager.alert("温馨提示","学员状态异常");
                return;
            }
            leaving_form.form("clear");
            leaving_dialog.dialog("setTitle", "学员流失");
            leaving_form.form("load", formalStudents);
            leaving_dialog.dialog("open");
        },
        save_leaving: function () { // 流失保存操作
            leaving_form.form("submit", {
                url: '/studentofficial/change2lost.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            leaving_dialog.dialog("close");
                            studentOfficial_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info");
                    }
                }
            });
        },
        cancel_leaving: function () {
            leaving_dialog.dialog("close");
        },
        //设置学员休学按钮
        quit:function(){

            var row = studentOfficial_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            if(!row.clientName){
                $.messager.alert("温馨提示","数据异常,学员名称不能为空!!!塞利木鸡拜");
                return;
            }
            var entity_state = "确认学员休学吗?";
            if (row.clientState.id == 44) {
                var entity_state = "确认学员复学吗?";
            }
            $.messager.confirm("温馨提示", entity_state, function (r) {
                if (r) {
                    $.get("/studentofficial/change2quit.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                studentOfficial_datagrid.datagrid("reload");
                            });
                        }
                    });
                }
            });
        },



        //高级查询按钮
        searchs: function () {
            var keyword = $("#keyword").textbox("getValue");
            var classNameId = $("#classNameId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            studentOfficial_datagrid.datagrid("load", {
                keyword: keyword,
                classNameId: classNameId,
                beginDate: beginDate,
                endDate: endDate
            });
        },

        //发送短信方法
        sendMsg:function () {
            var row = studentOfficial_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            console.log(row);
            if (row.clientName) {
                row["clientName.name"] = row.clientName.name
            }
            if (row.classname) {
                row["classname.name"] = row.classname.name
            }
            sendMsg_form.form("load", row);
            msgDialog.dialog("setTitle", "追数");
            msgDialog.dialog("open");
        },
        //发送短信表单的提交
        sendMsgIng:function () {
            sendMsg_form.form("submit", {
                url: "/studentofficial/sengMsg.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            msgDialog.dialog("close");
                            studentOfficial_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            msgDialog.dialog("close");
                        });
                    }
                }
            })
        }
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    studentOfficial_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/studentofficial/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_changeState").linkbutton({
                    text: "设置离职"
                });
            } else {
                $("#btn_changeState").linkbutton({
                    text: "设置复职"
                });

            }
        },
        columns: [[
            {
                field: 'clientName', title: '学员名称', width: 100, formatter: function (clientName) {
                    return clientName ? clientName.name : '';
                }
            },
            {
                field: 'saleman', title: '销售名称', width: 100, formatter: function (saleman) {
                    return saleman ? saleman.username : '';
                }
            },
            {field: 'totalAmount', title: '总学费', width: 100},
            {field: 'waitingFit', title: '待缴学费', width: 100},
            {field: 'payFit', title: '已交学费', width: 100},
            {field: 'payState', title: '缴费状态', width: 100, formatter: function (payState) {
                    return payState? "<font color='green'>已缴清</font>" : "<font color='red'>未缴清</font>";
                }
            },
            {field: 'admissionDate', title: '入学时间', width: 100},
            {field: 'school', title: '学校', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {
                field: 'classname', title: '所在班级', width: 100, formatter: function (classname) {
                    return classname ? classname.name : '';
                }
            },
            {
                field: 'payType', title: '付款方式', width: 100, formatter: function (payType) {
                    return payType ? payType.name : '';
                }
            },
            {
                field: 'clientType', title: '类型', width: 100, formatter: function (clientType) {
                    return clientType ? clientType.name : '';
                }
            },
            {
                field: 'clientState', title: '状态', width: 100, formatter: function (clientState) {
                    return clientState ? clientState.name : '';
                }
            }
        ]],
        toolbar: "#tools"
    });

    //编辑添加弹出框设置
    studentOfficial_dailog.dialog({
        width: 770,
        height: 500,
        closed: true,
        onClose: function () {
            studentOfficial_form.form("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                studentOfficial_dailog.dialog("close");
            }
        }]
    });

    changeClass_dialog.dialog({
        width: 300,
        height: 200,
        closed: true,
        onClose: function () {
            changeClass_dialog.form("clear");
        },
        buttons: [{
            text: '确定',
            iconCls: "icon-save",
            handler: methodObj["changeClassOK"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                changeClass_dialog.dialog("close");
            }
        }]
    })







    //短信编辑框信息
    msgDialog.dialog({
        width: 450,
        height: 340,
        closed: true,
        onClose: function () {
            sendMsg_form.form("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["sendMsgIng"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                msgDialog.dialog("close");
            }
        }]
    });

    //form表单设置
    studentOfficial_dailog.form({
        fit: true
    })
    changeClass_dialog.form({
        fit:true
    })

    //学员退学dialog
    leaving_dialog.dialog({
        width: 300,
        height: 350,
        buttons: "#leaving_btns",
        closed: true
    });

});

